package org.iabako.server.serviceimpl.report;

import org.apache.log4j.Logger;
import org.iabako.server.dao.report.InstallmentReportDAO;
import org.iabako.shared.dto.FinancesBalance;
import org.iabako.shared.dto.ReportGenerationInputDTO;
import org.iabako.shared.entity.business.AbstractEntity;
import org.iabako.shared.iservice.report.InstallmentReportGeneratorService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.Map;

/**
 * Created by jose on 27/01/15.
 */

@org.springframework.stereotype.Service("installmentReportGeneratorService")
public class InstallmentReportGeneratorServiceImpl extends AbstractReportService implements InstallmentReportGeneratorService {

    static Logger log = Logger.getLogger(InstallmentReportGeneratorServiceImpl.class.getName());

    @Autowired
    InstallmentReportDAO installmentReportDAO;

    @Override
    protected String getReportFileName() {
        return messages.getLabel("report_installments_filename");
    }

    @Override
    protected String getReportTitle() {
        return messages.getLabel("report_form_installments_title");
    }

    @Override
    protected String getColumnStyleLabel1() {
        return messages.getLabel("report_installments_historic_view");
    }

    @Override
    protected String getColumnStyleLabel2() {
        return messages.getLabel("report_installments_present_view");
    }

    public String generateInstallmentReport(ReportGenerationInputDTO reportGenerationInputDTO) {
        return generateReport(reportGenerationInputDTO);
    }

    @Override
    protected Map<Object, FinancesBalance> getDataMapByEntity(ReportGenerationInputDTO reportGenerationInputDTO, Date dateAfter, Date dateBefore) {
        return installmentReportDAO.getInstallmentsBy(reportGenerationInputDTO, dateAfter, dateBefore);
    }

    @Override
    protected Double getColumnValue1(FinancesBalance financesBalance) {
        boolean isPastInterval = new Date().compareTo(financesBalance.getFinanceBalanceDate()) > 0;
        return isPastInterval ? financesBalance.getSalesPayed() + financesBalance.getSalesReceivable() : 0;
    }

    @Override
    protected Double getColumnValue2(FinancesBalance financesBalance) {
        return financesBalance.getSalesReceivable();
    }

}
